package com.example.administrator.u17.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.u17.R;
import com.example.administrator.u17.activity.BeAuthorActivity;
import com.example.administrator.u17.activity.DYActivity;
import com.example.administrator.u17.activity.GameActivity;
import com.example.administrator.u17.activity.MainActivity;
import com.example.administrator.u17.activity.MineTitlePicActivity;
import com.example.administrator.u17.activity.SealActivity;
import com.example.administrator.u17.activity.SignActivity;
import com.example.administrator.u17.activity.YticketActivity;
import com.example.administrator.u17.db.MineGameDb;
import com.squareup.picasso.Picasso;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.io.InputStream;


/**
 * Created by Administrator on 2016/11/14.
 */

public class MineFragment extends Fragment implements View.OnClickListener {

    private TextView tvSpend, tvRechange, tvMyReading, tvSeal, tvAuthor, tvSign, tvGame, tvFeedBack, tvAboutUs;
    private ImageView ivTitlePic;;
    private static int REQUESTCODE=1616;
    private String mPath;
    private DbManager dbManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_mine, null);

        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void initView(View view) {

        tvAboutUs = (TextView) view.findViewById(R.id.tv_vip_aboutUs);
        tvAuthor = (TextView) view.findViewById(R.id.tv_vip_author);
        tvFeedBack = (TextView) view.findViewById(R.id.tv_vip_feedBack);
        tvGame = (TextView) view.findViewById(R.id.tv_vip_game);
        tvMyReading = (TextView) view.findViewById(R.id.tv_vip_subscribe);
        tvRechange = (TextView) view.findViewById(R.id.tv_vip_rechange);
        tvSpend = (TextView) view.findViewById(R.id.tv_vip_spend);
        tvSeal = (TextView) view.findViewById(R.id.tv_vip_seal);
        tvSign = (TextView) view.findViewById(R.id.tv_vip_sign);

//        tvSpend.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent=new Intent(getActivity(),YticketActivity.class);
//                getActivity().startActivity(intent);
//            }
//        });

        tvAuthor.setOnClickListener(this);
        tvFeedBack.setOnClickListener(this);
        tvGame.setOnClickListener(this);
        tvMyReading.setOnClickListener(this);
        tvRechange.setOnClickListener(this);
        tvSpend.setOnClickListener(this);
        tvSeal.setOnClickListener(this);
        tvSign.setOnClickListener(this);
        ivTitlePic= (ImageView) view.findViewById(R.id.iv_mine_pic);
        ivTitlePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), MineTitlePicActivity.class);
                startActivityForResult(intent,REQUESTCODE);
            }
        });

    }

    private void initData() {

        dbManager= x.getDb(new DbManager.DaoConfig());
        try {
            dbManager.save(new MineGameDb());
        } catch (DbException e) {
            e.printStackTrace();
        }


    }

    private void setData() {

//    Picasso.with(getActivity()).load(getPath()).into(ivTitlePic);

    }

    private void setListener() {


    }

    public String getPath(){

        Intent intent=getActivity().getIntent();
        if (intent!=null){
            mPath=intent.getStringExtra("mPath");
        }
        return mPath;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_vip_spend:
                Intent intent = new Intent(getActivity(), YticketActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_vip_rechange:
                Intent intent2 = new Intent(getActivity(), YticketActivity.class);
                startActivity(intent2);
                break;
            case R.id.tv_vip_subscribe://订阅
                Intent intent3 = new Intent(getActivity(), DYActivity.class);
                startActivity(intent3);
                break;
            case R.id.tv_vip_seal://封印
                Intent intent4 = new Intent(getActivity(),SealActivity.class);
                startActivity(intent4);
                break;
            case R.id.tv_vip_author://成为作者

                Intent intent5 = new Intent(getActivity(), BeAuthorActivity.class);
                startActivity(intent5);
                break;
            case R.id.tv_vip_sign://签到

                Intent intent6 = new Intent(getActivity(), SignActivity.class);
                startActivity(intent6);
                break;
            case R.id.tv_vip_game://游戏
                Intent intent7 = new Intent(getActivity(), GameActivity.class);
                startActivity(intent7);
                break;
            case R.id.tv_vip_feedBack://意见反馈
                Intent intent8 = new Intent(getActivity(), YticketActivity.class);
                startActivity(intent8);
                break;
            case R.id.tv_vip_aboutUs://关于我们
                Intent intent9 = new Intent(getActivity(), SealActivity.class);
                startActivity(intent9);
                break;


        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==REQUESTCODE &&resultCode==200)
        {
            //获取响应的数据内容
            String mPath=data.getStringExtra("mPath");
            MineGameDb gameDb=new MineGameDb();
            gameDb.mPath=mPath;

            BitmapFactory.Options options = new BitmapFactory.Options();
            // 只加载bitmap的宽高信息 不是真正的bitmap 而是一个空的 所以不会占用很大的内容
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(mPath, options);
            // 获取bitmap的宽高信息
            int outHeight = options.outHeight;
            int outWidth = options.outWidth;
            //算出压缩比
            int sample = Math.max(outHeight/200,outWidth/200);
            //设置压缩比
            options.inSampleSize = sample;
            //开始加载真正bitmap 并压缩
            options.inJustDecodeBounds = false;

            if (mPath!=null){
                Bitmap bitmap = BitmapFactory.decodeFile(mPath, options);
            }
            Bitmap bitmap = BitmapFactory.decodeFile(gameDb.mPath, options);

            if (bitmap!=null){

                ivTitlePic.setImageBitmap(bitmap);


            }

        }
    }
}
